package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/response"
	"awesomeProject2/utils"
	"github.com/gin-gonic/gin"
	"sort"
	"strings"
)

func getPaperScore(levelStr string) float64 {
	levels := strings.Split(levelStr, ",")
	intArr := utils.StringList2Int(levels)
	sort.Ints(intArr)

	max := intArr[len(intArr)-1]
	min := intArr[0]

	//	先判断是否为会议论文
	if max > 11 && max < 15 {
		return 40
	}

	// 根据论文等级统计分数
	if min > 0 && min <= 4 {
		return 50
	}

	if min > 4 && min <= 8 {
		return 40
	}

	if min > 8 && min <= 10 {
		return 15
	}

	return 5

}

func getPaperTotalScore(userId uint, year string) (totalScore float64) {
	var papers []model.Paper
	global.GVA_DB.Where("user_id = ? And year = ?", userId, year).Where("invalid_flag = 0").Find(&papers)
	for _, paper := range papers {
		score := getPaperScore(paper.PaperLevels)
		totalScore += score
	}
	return totalScore
}

func getWorkTotalScore(userId uint, year string) (totalScore float64) {
	var works []model.Work
	global.GVA_DB.Where("user_id = ? And year = ?", userId, year).Find(&works)
	for _, work := range works {
		var score float64
		if work.WorkLevel == "国家级" {
			score = work.NumberOfWords * 4
		} else {
			score = work.NumberOfWords * 2
		}
		totalScore += score
	}
	return totalScore
}

func getProjectTotalScore(userId uint, year string) (totalScore float64) {
	var projects []response.ProjectResponse

	global.GVA_DB.Model(&model.AnnualProjectRecord{}).Select("annual_project_record.*, project.*").
		Joins("left join project on project.id = annual_project_record.project_id").
		Where("project.user_id = ? AND annual_project_record.year = ?", userId, year).Find(&projects)

	for _, project := range projects {
		var score float64
		if project.ProjectCategory == "横向项目" {
			score = project.ProjectMoney * 10.5
		} else if strings.Contains(project.ProjectLevel, "国家") {
			score = project.ProjectMoney * 15
		} else if project.ProjectLevel == "省部级" {
			score = project.ProjectMoney * 13.5
		} else {
			score = project.ProjectMoney * 12
		}
		totalScore += score
	}
	return totalScore
}

func getPatentTotalScore(userId uint, year string) (totalScore float64) {
	var total int64
	global.GVA_DB.Model(&model.Patent{}).Where("user_id = ? And year = ?", userId, year).Count(&total)
	totalScore = float64(total * 100)
	return totalScore
}

func getAchievementTotalScore(userId uint, year string) (totalScore float64) {
	var achievements []model.Achievement
	global.GVA_DB.Where("user_id = ? And year = ?", userId, year).Find(&achievements)
	for _, achievement := range achievements {
		var score float64
		if achievement.AchievementLevel == "一等奖" {
			score = 150
		} else if achievement.AchievementLevel == "二等奖" {
			score = 100
		} else {
			score = 50
		}
		totalScore += score
	}
	return totalScore
}

func GetScore(c *gin.Context) {
	var reduce float64
	year := c.Param("year")
	_, teacher := model.GetCurrentTeacher(c)
	paperTotalScore := getPaperTotalScore(teacher.UserId, year)
	workTotalScore := getWorkTotalScore(teacher.UserId, year)
	projectTotalScore := getProjectTotalScore(teacher.UserId, year)
	achievementTotalScore := getAchievementTotalScore(teacher.UserId, year)
	patentTotalScore := getPatentTotalScore(teacher.UserId, year)

	total := patentTotalScore + paperTotalScore + workTotalScore + projectTotalScore + achievementTotalScore

	if teacher.PTitle == "教授" {
		reduce = 0.6 * total
	} else if teacher.PTitle == "副教授" {
		reduce = 0.75 * total
	} else {
		reduce = 1.0 * total
	}

	response.OkWithData(response.ScoreResponse{
		PaperTotalScore:       paperTotalScore,
		WorkTotalScore:        workTotalScore,
		ProjectTotalScore:     projectTotalScore,
		AchievementTotalScore: achievementTotalScore,
		PatentTotalScore:      patentTotalScore,
		TotalScore:            total,
		ReducedScore:          reduce,
	}, c)
}
